package br.mikhas.lang.struct;

import java.util.ArrayList;
import java.util.List;

import br.mikhas.lang.struct.code.Instruction;

public class Method extends AbstractAccessibleStructure {

	protected final String name;

	protected Class declaringClass;

	protected Type returnType;

	protected List<Parameter> parameters;

	protected List<Instruction> instructions;

	public Method(String name) {
		this.name = name;
	}

	/**
	 * Adds a new parameters to the method
	 * 
	 * @param parameter
	 */
	public void addParameter(Parameter parameter) {
		if (this.parameters == null)
			parameters = new ArrayList<Parameter>();
		parameter.index = (short) parameters.size();
		parameters.add(parameter);
	}

	public void addInstruction(Instruction instruction) {
		if (this.instructions == null)
			instructions = new ArrayList<Instruction>();
		instructions.add(instruction);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((declaringClass == null) ? 0 : declaringClass.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((parameters == null) ? 0 : parameters.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Method other = (Method) obj;
		if (declaringClass == null) {
			if (other.declaringClass != null)
				return false;
		} else if (!declaringClass.equals(other.declaringClass))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (parameters == null) {
			if (other.parameters != null)
				return false;
		} else if (!parameters.equals(other.parameters))
			return false;
		return true;
	}

}
